package com.example8.test_module.controller;

import com.example8.test_module.entity.ResponseEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.concurrent.TimeUnit;

/**
 * @Description 测试接口
 * @Author lihw
 * @Date 2024/1/9 13:06
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {


    @PostMapping("get_msg")
    public ResponseEntity testMsg() throws InterruptedException {
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setCode(0);
        responseEntity.setMsg("成功");
        TimeUnit.SECONDS.sleep(10L);
        if (true) {
            throw new RuntimeException("ddd");
        }
        return responseEntity;
    }

    @GetMapping("test2")
    public String msg() {
        WebClient.Builder builder = WebClient.builder();

        WebClient webClient = builder.baseUrl("http://127.0.0.1:8080").build();

        Mono<ResponseEntity> responseEntityMono = webClient.post()
                .uri("/test/get_msg")
                .retrieve()
                .bodyToMono(ResponseEntity.class);

        responseEntityMono.subscribe((responseEntity) -> {
            log.info("result: {}", responseEntity);
        }, err -> {
            log.info("错误信息: {}", err);
        });

        log.info("执行结束");
        return "succ";
    }
}
